<?php ob_start();
/**
 * Created by JetBrains PhpStorm.
 * User: David Evans
 * Date: 08/03/2012
 * Time: 20:11
 * To change this template use File | Settings | File Templates.
 */
require_once "configg.php";
$result=$db->select($connection,"SELECT * FROM menu ORDER BY id");
foreach($result as $value){
    @$menuData['items'][$value['id']]=$value;
    @$menuData['parent'][$value['parent']][]=$value['id'];
 }
function select_menu($parent,$menuData,$text="--"){
        $html="";
        if(isset($menuData['parent'][$parent])){
            foreach($menuData['parent'][$parent] as $value){
                $html.="<option value='{$value}'>";
                $html.=$text.$menuData['items'][$value]['name'];
                $html.="</option>";
                $html.= select_menu($value,$menuData,$text."--");
            }
        }
        return $html;
 }
if(isset($_POST['submit'])){

        if(!empty($_POST['menu'])){
            $menu_name=addslashes($_POST['menu']);
            $parent=addslashes($_POST['menu_id']);
			$link=addslashes($_POST['link']);

            if($db->insert($connection,"INSERT INTO menu(name,parent,link_lienket) VALUE('{$menu_name}',{$parent},'{$link}')")==true){
                $insert="Thanh cong";
				header("location:../MyModel/TrangAD_themmenu.php");
            }
            else $insert="That bai";
        }
        else echo "Vui long dien vao";
}
	
?>
<form action="add_menu.php" method="post">
    <table>
    <tr>
    	<td>Parent Node</td>
        <td>Node Name</td>
        <td>Link</td>
    </tr>
    <tr>
    	<td>
        	<select name="menu_id">
                <option value="0">None</option>
                <?php echo select_menu(0,$menuData);?>
        	</select>
        </td>
        <td>
        	<input type="text" name="menu"/>
        </td>
        <td>
        	<input type="text" name="link"/>
        </td>
    </tr>
    </table>
    <input type="submit" name="submit"/>
</form>
<p><?php if(isset($insert)) echo $insert;?></p>
